import java.io.*;
public class PasswordManager {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer sc = new StreamTokenizer(br);
    static int ini() throws IOException{
        sc.nextToken();
        return (int)sc.nval;
    }

    public static boolean check(String password){
        String regex = ".*[^a-zA-Z0-9].*";
        if(password.matches(regex)||password.length()>16){
            return true;
        }
        return false;
    }
    public static String encrypt(String password) {

        StringBuilder encrypt = new StringBuilder();

        for (int i = 0; i < password.length(); i++) {
            char character = password.charAt(i);
            int asciiCode = (int) character + (i + 1) + 3;
            encrypt.append((char) asciiCode);
        }

        int length = encrypt.length();
        char firstChar = encrypt.charAt(0);
        char lastChar = encrypt.charAt(length - 1);

        encrypt.setCharAt(0, lastChar);
        encrypt.setCharAt(length - 1, firstChar);

        return encrypt.reverse().toString();
    }

    public static String decrypt(String encryptedPassword) {
        if(encryptedPassword.length()==1){
            char character = encryptedPassword.charAt(0);
            int asciiCode = (int) character - 4;
            return String.valueOf((char) asciiCode);
        }
        String reversedPassword = new StringBuilder(encryptedPassword).reverse().toString();
        int length = reversedPassword.length();
        char first = reversedPassword.charAt(0);
        char last = reversedPassword.charAt(length - 1);

        reversedPassword = last + reversedPassword.substring(1, length - 1) + first;

        StringBuilder decrypted = new StringBuilder();

        for (int i = 0; i < reversedPassword.length(); i++) {
            char character = reversedPassword.charAt(i);
            int asciiCode = (int) character - (i + 1) - 3;
            decrypted.append((char) asciiCode);
        }

        return decrypted.toString();
    }

    // 判断密码强度
    private static void checkPassword(String password) {

        if (password.length() < 8 || password.matches("[0-9]+") || password.matches("[a-zA-Z]+")) {
            System.out.println("弱强度密码");
        } else if (password.matches(".*\\d.*") && password.matches(".*[a-z].*") && password.matches(".*[A-Z].*")) {
            System.out.println("高强度密码");
        } else if (password.matches("(?=.*[0-9]).+") && password.matches(".*[a-zA-Z].*")) {
            System.out.println("中强度密码");
        } else {
            System.out.println("未知强度密码");
        }

    }

    // 生成密码
    private static String generatePassword() throws IOException {
        int length = ini();

        if (length <= 0) {
            return "无效的密码长度";
        }

        StringBuilder generatedPassword = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char c = (char) ((Math.random() * 26) + 'a');
            generatedPassword.append(c);
        }

        return generatedPassword.toString();
    }

    public static void main(String[] args) throws IOException {

        while (true) {
            System.out.println("==============================\n欢迎使用密码管理系统\n==============================\n请选择操作：\n1. 加密\n2. 解密\n3. 判断密码强度\n4. 生成密码\n5. 退出\n请输入选项序号：");

            int choice = ini();

            switch (choice) {
                case 1:
                    System.out.println("==============================\n请输入要加密的密码：");
                    String toEncrypt = br.readLine();
                    while (check(toEncrypt)) {
                        System.out.println("==============================\n你输入的密码不符合规则，请重新输入要加密的密码：");
                        toEncrypt = br.readLine();
                    }
                    String encrypt = encrypt(toEncrypt);
                    System.out.println("加密后的结果是： " + encrypt);
                    System.out.println("按任意键返回");
                    br.readLine();
                    break;
                case 2:
                    System.out.println("==============================\n请输入要解密的密码：");
                    String toDecrypt = br.readLine();
                    while (check(toDecrypt)) {
                        System.out.println("==============================\n你输入的密码不符合规则，请重新输入要解密的密码：");
                        toDecrypt = br.readLine();
                    }
                    String decrypt = decrypt(toDecrypt);
                    System.out.println("解密后的结果是： " + decrypt);
                    System.out.println("按任意键返回");
                    br.readLine();
                    break;
                case 3:
                    System.out.println("==============================\n请输入要判断密码强度的密码：");
                    String toCheckPassword = br.readLine();
                    checkPassword(toCheckPassword);
                    System.out.println("按任意键返回");
                    br.readLine();
                    break;
                case 4:
                    System.out.print("请输入密码长度：");
                    System.out.println(generatePassword());
                    break;
                case 5:
                    System.out.println("感谢你的使用，欢迎再次使用！");
                    br.close();
                    System.exit(0);
                default:
                    System.out.println("无效的选项，请重新选择。");
            }
        }
    }
}
